﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Reflection;

namespace Steve.UI
{
    [AttributeUsage(AttributeTargets.Method, Inherited=true, AllowMultiple=false)]
    public sealed class RouteValuePresentAttribute : ActionMethodSelectorAttribute
    {
        public string RouteValueKey { get; private set; }

        public RouteValuePresentAttribute(string routeValueKey)
        {
            if (string.IsNullOrWhiteSpace(routeValueKey)) throw new ArgumentNullException("routeValueKey");

            RouteValueKey = routeValueKey;
        }

        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
        {
            if (controllerContext == null) throw new ArgumentNullException("controllerContext");

            var values = controllerContext.RouteData.Values;
            return values.ContainsKey(RouteValueKey) && values[RouteValueKey] != null;
        }
    }

    [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public sealed class RouteValueAbsentAttribute : ActionMethodSelectorAttribute
    {
        public string RouteValueKey { get; private set; }

        public RouteValueAbsentAttribute(string routeValueKey)
        {
            if (string.IsNullOrWhiteSpace(routeValueKey)) throw new ArgumentNullException("routeValueKey");

            RouteValueKey = routeValueKey;
        }

        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
        {
            if (controllerContext == null) throw new ArgumentNullException("controllerContext");

            var values = controllerContext.RouteData.Values;
            return !values.ContainsKey(RouteValueKey) || values[RouteValueKey] == null;
        }
    }
}
